package com.gs.m_order_list.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.gs.common.data.Order
import com.gs.common.util.DateUtil
import com.gs.m_order_list.R


class OrderListAdapter(context: Context, data: ArrayList<Order>): RecyclerView.Adapter<OrderListAdapter.OrderListViewHolder>() {

    private val data: ArrayList<Order>
    private val context: Context

    private var mOnItemClickListener: OnOrderListItemClickListener? = null
    private var mOnItemDeleteBtnClickListener:  OnOrderListItemDeleteBtnClickListener? = null

    inner class OrderListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val orderDate: TextView
        val orderId: TextView
        val order1: RelativeLayout
        val order2: RelativeLayout
        val order3: RelativeLayout
        val good1Title: TextView
        val good2Title: TextView
        val good3Title: TextView
        val good1Count: TextView
        val good2Count: TextView
        val good3Count: TextView
        val orderAllPay: TextView
        val orderAllPayActual: TextView
        val deleteOrderBtn: TextView

        init {
            orderDate = itemView.findViewById(R.id.order_date_tv)
            orderId = itemView.findViewById(R.id.order_id_tv)
            order1 = itemView.findViewById(R.id.order1_layout)
            order2 = itemView.findViewById(R.id.order2_layout)
            order3 = itemView.findViewById(R.id.order3_layout)
            good1Title = itemView.findViewById(R.id.good1_title_tv)
            good2Title = itemView.findViewById(R.id.good2_title_tv)
            good3Title = itemView.findViewById(R.id.good3_title_tv)
            good1Count = itemView.findViewById(R.id.good1_count_tv)
            good2Count = itemView.findViewById(R.id.good2_count_tv)
            good3Count = itemView.findViewById(R.id.good3_count_tv)
            orderAllPay = itemView.findViewById(R.id.order_all_pay)
            orderAllPayActual = itemView.findViewById(R.id.order_all_pay_actual)
            deleteOrderBtn = itemView.findViewById(R.id.delete_order_btn)

            deleteOrderBtn.setOnClickListener {
                if (data.size == 0) return@setOnClickListener
                mOnItemDeleteBtnClickListener?.onClick(adapterPosition, data[adapterPosition].id)
            }

            itemView.setOnClickListener {
                mOnItemClickListener?.onItemClick(adapterPosition, data[adapterPosition].id)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OrderListViewHolder {
        val view = View.inflate(context, R.layout.order_list_item_layout, null)
        return OrderListViewHolder(view)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: OrderListViewHolder, position: Int) {
        holder.orderDate.text = "* ${DateUtil.dateToStringYMDhhmm(data[position].pay.create_time)} *"
        holder.orderId.text = data[position].id.toString()
        holder.orderAllPay.text = data[position].total_money.toString()
        holder.orderAllPayActual.text = data[position].pay.pay_money.toString()
        val orderDetails = data[position].orderDetailsList
        when(orderDetails.size) {
            1 -> {
                holder.order1.visibility = View.VISIBLE
                holder.order2.visibility = View.GONE
                holder.order3.visibility = View.GONE
                holder.good1Title.text = data[position].orderDetailsList[0].goods.title
                holder.good1Count.text = "x${data[position].orderDetailsList[0].goods_count}"
            }
            2 -> {
                holder.order1.visibility = View.VISIBLE
                holder.order2.visibility = View.VISIBLE
                holder.order3.visibility = View.GONE
                holder.good1Title.text = data[position].orderDetailsList[0].goods.title
                holder.good1Count.text = "x${data[position].orderDetailsList[0].goods_count}"
                holder.good2Title.text = data[position].orderDetailsList[1].goods.title
                holder.good2Count.text = "x${data[position].orderDetailsList[1].goods_count}"
            }
            else -> {
                holder.order1.visibility = View.VISIBLE
                holder.order2.visibility = View.VISIBLE
                holder.order3.visibility = View.VISIBLE
                holder.good1Title.text = data[position].orderDetailsList[0].goods.title
                holder.good1Count.text = "x${data[position].orderDetailsList[0].goods_count}"
                holder.good2Title.text = data[position].orderDetailsList[1].goods.title
                holder.good2Count.text = "x${data[position].orderDetailsList[1].goods_count}"
                holder.good3Title.text = data[position].orderDetailsList[2].goods.title
                holder.good3Count.text = "x${data[position].orderDetailsList[2].goods_count}"
            }
        }
    }

    interface OnOrderListItemClickListener {
        fun onItemClick(position: Int, orderId: Int)
    }

    interface OnOrderListItemDeleteBtnClickListener {
        fun onClick(position: Int, orderId: Int)
    }

    fun setOrderListItemClickListener(listener: OnOrderListItemClickListener) {
        mOnItemClickListener = listener
    }

    fun setOrderListItemDeleteBtnClickListener(listener: OnOrderListItemDeleteBtnClickListener) {
        mOnItemDeleteBtnClickListener = listener
    }

    fun removeOrderListItemClickListener() {
        if (mOnItemClickListener != null) {
            mOnItemClickListener = null
        }
    }

    fun removeOrderListItemDeleteBtnClickListener() {
        if (mOnItemDeleteBtnClickListener != null) {
            mOnItemDeleteBtnClickListener = null
        }
    }


    init {
        this.data = data
        this.context = context
    }

}